//
//  QITools.m
//  南宁飞鲸语音
//
//  Created by Q on 2023/2/27.
//

#import "QITools.h"

@implementation QITools




/// 获取顶部安全区域
+ (CGFloat) getStatusHeight {
    CGFloat statusBarHeight = 0;
    if (@available(iOS 13.0, *)) {
          NSSet *set = [UIApplication sharedApplication].connectedScenes;
          UIWindowScene *windowScene = [set anyObject];
          UIWindow *window = windowScene.windows.firstObject;
          return window.safeAreaInsets.top;
      } else if (@available(iOS 11.0, *)) {
          UIWindow *window = [UIApplication sharedApplication].windows.firstObject;
          return window.safeAreaInsets.top;
      }
      return 0;
}

/// 获取底部安全区域
+ (CGFloat)getBottomHeight {
    if (@available(iOS 13.0, *)) {
        NSSet *set = [UIApplication sharedApplication].connectedScenes;
        UIWindowScene *windowScene = [set anyObject];
        UIWindow *window = windowScene.windows.firstObject;
        return window.safeAreaInsets.bottom;
    } else if (@available(iOS 11.0, *)) {
        UIWindow *window = [UIApplication sharedApplication].windows.firstObject;
        return window.safeAreaInsets.bottom;
    }
    return 0;
}


/*
 * 日期转字符串
 * date : 需要转换的日期
 */
-(NSString * )theTargetDateConversionStr:(NSDate * )date dateFormat: (NSString *) formatStr
{
    NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init];//实例化一个NSDateFormatter对象
//    [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];//设定时间格式,这里可以设置成自己需要的格式
    [dateFormat setDateFormat:formatStr];//设定时间格式,这里可以设置成自己需要的格式
    NSString *currentDateStr = [dateFormat stringFromDate:date];
    // 根据自己需求处理字符串
    return [currentDateStr substringToIndex:7];
}
// 字符串转date
-(NSDate* )theTargetStringConversionDate:(NSString *)str dateFormat: (NSString *) formatStr
{
    //设置转换格式
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
//    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    [formatter setDateFormat:formatStr];
    //NSString转NSDate
    NSDate *date=[formatter dateFromString:str];
    return date;
}
// 传入一个日期返回这个日期的 NSDateComponents对象
-(NSDateComponents *)aCertainDateComponents:(NSDate *)date{
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSCalendarUnit _dayInfoUnits  = NSCalendarUnitEra | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
    NSDateComponents *components = [calendar components:_dayInfoUnits fromDate:date];
    return components;
}
/*
 *  获取当前月天数
 */
-(NSInteger)getCurrentMonthForDays
{
    NSCalendar *currentCalendar = [NSCalendar currentCalendar];
    NSRange range = [currentCalendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:[NSDate date]];
    NSUInteger numberOfDaysInMonth = range.length;
    NSLog(@"nsrange = %@----- %ld",NSStringFromRange(range),range.location);
    return numberOfDaysInMonth;
}
/*
 *  date :当前时间
 *  number:当前月之后几个个月的1号date
 */
-(NSDate *)getAMonthframDate:(NSDate*)date months:(NSInteger)number{
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSCalendarUnit _dayInfoUnits  = NSCalendarUnitEra | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
    NSDateComponents *components = [calendar components:_dayInfoUnits fromDate:date];
    components.day = 1;
    if (number!=0) {
      components.month += number;
    }
    NSDate * nextMonthDate =[calendar dateFromComponents:components];
    return nextMonthDate;
}
/*
 * 获取某个月一共多少天
 * date 要获取的月份的date
 */
-(NSInteger)getNextNMonthForDays:(NSDate * )date
{
    NSInteger monthNum =[[NSCalendar currentCalendar] rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:date].length;
//     = (NSInteger)[date YHBaseNumberOfDaysInCurrentMonth];
    return monthNum;
}
/**
 *  获取某个月的1号是星期几
 *  date 目标月的date
 **/
-(NSInteger)getFirstDayWeekForMonth:(NSDate*)date
{
#warning mark -- 有待优化，不知道为什么星期数总是比真实多一天（暂时用➖一天处理了）
//    NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; // 指定日历的算法 NSCalendarIdentifierGregorian,NSGregorianCalendar
    // NSDateComponent 可以获得日期的详细信息，即日期的组成
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *comps = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond|NSCalendarUnitWeekday fromDate:date];
//    NSLog(@"comps是这个样子的:%@",comps);
    NSInteger weekday = [comps weekday];
    weekday--;
//    NSLog(@"[comps weekday] = %ld",(long)weekday);
    if (weekday == 7) {
        return 0;
    }else return weekday;
}

@end
